﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using OConf.Common;
using OConf.Entity;
using OConf.Model.Common;

namespace OConf.API.Filter
{
    /// <summary>
    /// 接口验权标签
    /// </summary>
    public class PermissionForApiAttribute : ActionFilterAttribute
    {
        public PermissionForApiAttribute()
        {
            Order = 1;
        }
        /// <summary>
        /// 运行访问接口最低角色类型
        /// </summary>
        public RoleTypeEnum AllowRole { get; set; } = Entity.RoleTypeEnum.超级管理员;
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            int roleType = ManagerUnit.GetCurrentRoleType();
            if (roleType <= 0)
            {
                ReturnData<object> result = new ReturnData<object>()
                {
                    Code = (int)VabStateCode.StatusCode.令牌过期,
                    Msg = VabStateCode.StatusCode.令牌过期.ToString(),
                    Data = null
                };
                context.Result = new OkObjectResult(result);
            }

            if (roleType > (int)AllowRole)
            {
                ReturnData<object> result = new ReturnData<object>()
                {
                    Code = (int)VabStateCode.StatusCode.用户没有权限,
                    Msg = VabStateCode.StatusCode.用户没有权限.ToString(),
                    Data = null
                };
                context.Result = new OkObjectResult(result);
            }
        }
    }
}
